#pragma once
#include "vector3.h"
#include "quaternion.h"
#include "md5_parser.h"
#include "md5_nodes.h"
#include <string>

namespace krryn{
namespace data{
	class md5_anim_parser : public md5_parser{
	public:
		md5_anim_parser(const std::string &a_Filename, const std::string &a_Name = "", size_t a_MemoryBudgetBytes = 4096 * 1024);
		virtual ~md5_anim_parser();

		std::string get_name() const { return m_Name; }
		md5_animation &get_animation() { return m_Animation; }

		virtual void parse();
	private:
		std::string m_Name;
		md5_animation m_Animation;

		enum{
			pos_x = 1,
			pos_y = 2,
			pos_z = 4,
			rot_x = 8,
			rot_y = 16,
			rot_z = 32
		};

		struct joint_data{
			std::string m_Name;
			int m_Parent;
			int m_Flags;
			int m_StartIndex;
		};

		struct base_frame_data{
			math::vector3f    m_Position;
			math::quaternionf m_Rotation;
		};

		/// temporary data that can be cleaned up as soon as we're done parsing
		joint_data *m_JointData;
		base_frame_data *m_BaseFrameData;
		float *m_FrameData;
		int m_NumAnimatedComponents;

		void settings();
		void hierarchy_block();
		void joints(int a_Index);
		void bounds_block();
		void bounds_data(int a_Index);
		void baseframe_block();
		void baseframe_data(int a_Index);
		void frame_block();
		void build_frame(int a_Index);
		int set_data(int a_Index, int a_Mask, int a_Offset, float &a_Result);
		bool is_valid_eof();
	};
}
}